import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

/**
 * Created by WEN on 2016/9/27.
 */
public class SingleNumber {
    public int singleNumber(int[] nums) {
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        int index;
        for (int i=0; i<nums.length;i++){
            index = arrayList.indexOf(nums[i]);
            if(index!=-1){
                arrayList.remove(index);
            }else
            arrayList.add(nums[i]);
        }
        return arrayList.get(0);
    }
    public int singleNumber2(int[] nums){

        for (int i =0; i<nums.length;i++){
            int flag=0;
            for(int j = 0; j<nums.length;j++){
                if(j==i) continue;
                if(nums[i]==nums[j]) {
                    flag=1;
                    break;
                }
            }
            if(flag==0) return nums[i];
        }
        return 0;
    }
    public int singleNumber3(int[] A) {
        int x = 0;
        for (int a : A) {
            x = x ^ a;
        }
        return x;
    }
    public int singleNumber4(int[] A) {
        HashSet<Integer> set = new HashSet<Integer>();
        for (int n : A) {
            if (!set.add(n))
                set.remove(n);
        }
        Iterator<Integer> it = set.iterator();
        return it.next();
    }
}
